<?php
namespace app\common\controller;

use PHPExcel;
use PHPExcel_IOFactory;

class ExportExcel
{
    protected $age_status = [
        '0' => '未认证',
        '1' => '未成年',
        '2' => '已成年',
    ];

    public function exportExcel(){
        $data = $_REQUEST;
        $type = $data["type"];
        ini_set('memory_limit', '512M');
        set_time_limit(0);
        switch ($type){
            case 'user':
                $expTitle = "玩家列表";
                $expCellName = array(
                    array('id','玩家ID'),
                    array('account','玩家帐号'),
                    array('game_name','注册游戏'),
                    array('channel','所属渠道'),
                    array('login_time','最后登录时间'),
                    array('login_ip','登录IP'),
                    array('status','账号状态'),
                    array('type','注册方式'),
                    array('is_age','是否实名认证'),
                );
                $map = [];
                if(!empty($data['ip'])){
                    $map['login_ip'] = array('like','%'.$data['ip'].'%');
                }
                if(!empty($data['game'])){
                    $map['gid'] = $data['game'];
                }
                if (!empty($data['starttime']) && !empty($data['endtime'])) {
                    $map[]= ['login_time','between time',[strtotime($data['starttime']),strtotime($data['endtime'])]];
                }elseif (!empty($data['starttime'])) {
                    $map[] = ['login_time','>=',strtotime($data['starttime'])];
                }elseif (!empty($data['endtime'])) {
                    $map[] = ['login_time','<=',strtotime($data['endtime'])];
                }
                $expTableData = \app\common\model\Player::field('id,account,gid,cid,login_time,login_ip,status,type,is_age')->where($map)->select()->toArray();
                foreach ($expTableData as &$key){
                    $key['game_name'] = get_game_filed($key['gid'],'name');
                    $key['channel'] = get_channel_name($key['cid'],'nickname');
                    $key['login_time'] = date("Y-m-d H:i:s",$key['login_time']);
                    $key['status'] = $key['status'] == 1 ? '正常' : '禁用';
                    $key['type'] = $key['type'] == 1 ? '手机号注册' : '账号注册';
                    $key['is_age'] = $this->age_status[$key['is_age']];
                    unset($key['gid']);
                    unset($key['cid']);
                }
                break;
        }
        self::exportExcel($expTitle,$expCellName,$expTableData);
    }

    private static function export($expTitle,$expCellName,$expTableData){
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = session('user_auth.username').date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        Vendor("PHPExcel.PHPExcel");
        $objPHPExcel = new \PHPExcel();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
        }
        //数据处理
        foreach ($expTableData as $key => $val) {
            foreach ($expCellName as $k => $v) {
                if (isset($v[2])) {
                    $ar_k = array_search('*', $v);
                    if ($ar_k !== false) {
                        $v[$ar_k] = $val[$v[0]];
                    }
                    $fun = $v[2];
                    $param = $v;
                    unset($param[0], $param[1], $param[2]);
                    $val[$v[0]] = call_user_func_array($fun, $param);
                }
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$k].($key+3), $val[$v[0]]);
            }
        }
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
        header("Content-Disposition:attachment;filename=$xlsTitle.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }
}
